package com.example.administrator.printoutindemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.example.administrator.printoutindemo.database.SQLiteDBService;


public class WebserviceConfigActivity extends Activity {

	private static final String WEBSERVICE_CONFIG = "webServiceInfo";
	private EditText txtIp = null;
	private EditText txtPort = null;
	private Button btnSave = null;
	private Button btnQuit = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.webserviceconfig);

		txtIp = (EditText) findViewById(R.id.txtIp);
		txtPort = (EditText) findViewById(R.id.txtPort);
		btnSave = (Button) findViewById(R.id.btnSave);
		btnQuit = (Button) findViewById(R.id.btnQuit);

		SharedPreferences sPreferencces = getSharedPreferences(
				WEBSERVICE_CONFIG, 0);

		txtIp.setText(sPreferencces.getString("ip", ""));
		txtPort.setText(sPreferencces.getString("Port", ""));

		btnSave.setOnClickListener(new btnSaveClick());
		btnQuit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent logoutIntent = new Intent(WebserviceConfigActivity.this,
						LoginActivity.class);
				logoutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
						| Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(logoutIntent);
			}
		});

	}

	private class btnSaveClick implements OnClickListener {
		@Override
		public void onClick(View v) {

			try {
				if (txtIp.getText().toString().length() == 0) {

					new AlertDialog.Builder(WebserviceConfigActivity.this)
							.setTitle("提示")
							.setMessage("请输入web服务ip地址")
							.setPositiveButton("确定",
									new DialogInterface.OnClickListener() {

										@Override
										public void onClick(
												DialogInterface arg0, int arg1) {

										}
									}).show();

				} else {

					// 建立本地SQLite数据库
					new SQLiteDBService(WebserviceConfigActivity.this)
							.onUpgrade();

					saveData();
				}
			} catch (Exception e) {
				// TODO: handle exception
			}

		}
	}

	private void saveData() {

		// new WebserviceInfoMan().saveData(txtIp.getText().toString(),
		// txtPort.getText().toString());

		SharedPreferences sPreferencces = getSharedPreferences(
				WEBSERVICE_CONFIG, 0);

		Editor editor = sPreferencces.edit();
		editor.putString("ip", txtIp.getText().toString());
		editor.putString("Port", txtPort.getText().toString());
		editor.commit();

		new AlertDialog.Builder(WebserviceConfigActivity.this).setTitle("提示")
				.setMessage("保存成功！")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int arg1) {

						Intent intent = new Intent();
						intent.setClass(WebserviceConfigActivity.this,
								LoginActivity.class);
						startActivity(intent);
					}
				}).show();

	}

}